package com.songlanyun.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.modules.account.dao.AccountOperationLogDao;
import com.songlanyun.modules.account.entity.AccountOperationLogEntity;
import com.songlanyun.modules.account.model.vo.AccountOperationLogVO;
import com.songlanyun.modules.account.service.AccountOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户管理的操作日志的服务实现
 *
 * @author 王熙伟(wang-xiwei@foxmail.com)
 * @since 2024-04-24
 */
@Service("yxtAccountOperationLogService")
public class AccountOperationLogServiceImpl extends ServiceImpl<AccountOperationLogDao, AccountOperationLogEntity>
        implements AccountOperationLogService {

    /**
     * 保存一个实体。
     *
     * @param entity 待保存的实体
     * @return 如果保存成功则返回保存后的实体否则返回null。
     */
    @Override
    public AccountOperationLogEntity create(AccountOperationLogEntity entity) {
        int effectedRowCount = this.baseMapper.insert(entity);
        if(effectedRowCount > 0){
            return entity;
        } else {
            return null;
        }
    }

    /**
     * 修改一个实体
     *
     * @param entity 待修改的实体
     * @return 修改后的实体
     */
    @Override
    public AccountOperationLogEntity modify(AccountOperationLogEntity entity) {
        throw new RuntimeException("Forbidden to modify any log!");
    }

    /**
     * 移除一个实体
     *
     * @param id 待移除的实体之ID
     */
    @Override
    public void remove(Long id) {
        throw new RuntimeException("Forbidden to remove any log!");
    }

    /**
     * 分页查询实体列表
     *
     * @param params 若干查询条件
     * @return 和查询条件匹配的实体列表
     */
    @Override
    public PageUtils queryPage(@NotNull Map<String, Object> params) {
        LambdaQueryWrapper<AccountOperationLogEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        // 处理查询参数：userId
        String userId = (String) params.get("userId");
        if(StringUtils.isNotBlank(userId)){
            queryWrapper.like(AccountOperationLogEntity::getUserId, "%" + userId + "%");
        }

        // 处理查询参数：userName
        String userName = (String) params.get("userName");
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.like(AccountOperationLogEntity::getUserName, "%" + userName + "%");
        }

        // 处理查询参数：startTime
        Date startTime = (Date) params.get("startTime");
        if(startTime != null){
            queryWrapper.ge(AccountOperationLogEntity::getCreateTime, startTime);
        }

        // 处理查询参数：startTime
        Date endTime = (Date) params.get("endTime");
        if(endTime != null){
            queryWrapper.le(AccountOperationLogEntity::getCreateTime, endTime);
        }

        // 处理查询参数：limit
        String limit = String.valueOf(params.get("limit"));
        long limitVal = 10L;
        if(StringUtils.isNotBlank(limit)) {
            try{
                limitVal = Long.parseLong(limit);
            } catch (NumberFormatException e) {
                //
            }
        }

        // 处理查询参数：page
        String page = String.valueOf(params.get("page"));
        long pageVal = 1L;
        if(StringUtils.isNotBlank(page)) {
            try{
                pageVal = Long.parseLong(page);
            } catch (NumberFormatException e) {
                //
            }
        }

        // 处理查询参数：sort_type
        String sortType = String.valueOf(params.get("sort_type"));
        if(StringUtils.equalsIgnoreCase("ASC", sortType)){
            queryWrapper.orderByAsc(AccountOperationLogEntity::getCreateTime);
        } else if(StringUtils.equalsIgnoreCase("DESC", sortType)){
            queryWrapper.orderByDesc(AccountOperationLogEntity::getCreateTime);
        } else {
            // 设置默认的排序字段
            queryWrapper.orderByDesc(AccountOperationLogEntity::getCreateTime);
        }

        // 设置分页参数
        Page<AccountOperationLogEntity> queryPage = new Page<>((pageVal - 1) , limitVal);

        // 查询结果并返回
        IPage<AccountOperationLogEntity> resultPage = this.baseMapper.selectPage(queryPage, queryWrapper);
        return new PageUtils(resultPage);
    }

    /**
     * 会员操作日志导出之列表查询
     *
     * @param params 查询参数
     * @return 和查询参数想匹配的日志列表
     */
    @Override
    public List<AccountOperationLogVO> exportList(Map<String, Object> params) {
        return this.baseMapper.exportList(params);

        /*
        LambdaQueryWrapper<AccountOperationLogEntity> queryWrapper = new LambdaQueryWrapper<>();

        // 处理查询参数：userId
        String userId = (String) params.get("userId");
        if(StringUtils.isNotBlank(userId)){
            queryWrapper.like(AccountOperationLogEntity::getUserId, "%" + userId + "%");
        }

        // 处理查询参数：userName
        String userName = (String) params.get("userName");
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.like(AccountOperationLogEntity::getUserName, "%" + userName + "%");
        }

        // 处理查询参数：startTime
        Date startTime = (Date) params.get("startTime");
        if(startTime != null){
            queryWrapper.ge(AccountOperationLogEntity::getCreateTime, startTime);
        }

        // 处理查询参数：startTime
        Date endTime = (Date) params.get("endTime");
        if(endTime != null){
            queryWrapper.le(AccountOperationLogEntity::getCreateTime, endTime);
        }

        // 处理查询参数：sort_type
        String sortType = String.valueOf(params.get("sort_type"));
        if(StringUtils.equalsIgnoreCase("ASC", sortType)){
            queryWrapper.orderByAsc(AccountOperationLogEntity::getCreateTime);
        } else if(StringUtils.equalsIgnoreCase("DESC", sortType)){
            queryWrapper.orderByDesc(AccountOperationLogEntity::getCreateTime);
        } else {
            // 设置默认的排序字段
            queryWrapper.orderByDesc(AccountOperationLogEntity::getCreateTime);
        }

        // 查询结果并返回
        return this.baseMapper.selectList(queryWrapper);

         */
    }
}
